* The analysis was conducted using Stata 16. 
clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"
use DatasetDSBC, clear

*This is an out-of-sample estimation. Only keep those trials not used for the estimation
keep if idleft !=5 & idright !=5

*Do this for each utilit function and noise specification (RUM-CARA, RUM-CRRA, RPM-CARA, RPM-CRRA)
*Merge estimated risk attitudes
merge m:1 subj using individualiskNoTimePressureBarsCARA, nogen
merge m:1 subj using individualisTimePressureBarsCARA, nogen
merge m:1 subj using individualiskNoTimePressurePieCARA, nogen
merge m:1 subj using individualisTimePressurePieCARA, nogen

*Collapse the dataset at the subject and pair level
collapse prob1 prob2 out1 out2 choiceleft Pie nopressure rhat_YesP_Bars rhat_NoP_Bars rhat_YesP_Pies rhat_NoP_Pies noise_YesP_Bars noise_NoP_Bars noise_YesP_Pies noise_NoP_Pies, by(subj idpair)

preserve
***************************************************************
*Change the name of these variables for each utilit function or noise specification!
***************************************************************
gen rhatCARA_YesP_Bars = rhat_YesP_Bars 
gen rhatCARA_NoP_Bars = rhat_NoP_Bars
gen rhatCARA_YesP_Pies =  rhat_YesP_Pies 
gen rhatCARA_NoP_Pies =  rhat_NoP_Pies 
gen noiseCARA_YesP_Bars =  noise_YesP_Bars 
gen noiseCARA_NoP_Bars = noise_NoP_Bars 
gen noiseCARA_YesP_Pies = noise_YesP_Pies
gen noiseCARA_NoP_Pies = noise_NoP_Pies
collapse rhatCARA_YesP_Bars rhatCARA_NoP_Bars rhatCARA_YesP_Pies rhatCARA_NoP_Pies noiseCARA_YesP_Bars noiseCARA_NoP_Bars noiseCARA_YesP_Pies noiseCARA_NoP_Pies , by(subj)
*Save this for Th.2 predictions
save riskattitudesCARA, replace 
restore

*variables for normalization
egen m1 = max(out1)
egen m2 = max(out2)
gen maxOut=max(m1,m2)

*calculate choices from choice proportions
tab choiceleft, missing
replace choiceleft=1 if choiceleft>=0.5
replace choiceleft=0 if choiceleft<0.5

*Calculate EU and correctly predicted choices out-of-sample for each condition
gen EUAYesP_Bars= (prob1*((1-exp(-rhat_YesP_Bars*out1))/(1-exp(-rhat_YesP_Bars*(maxOut))))) 
gen EUBYesP_Bars= (prob2*((1-exp(-rhat_YesP_Bars*out2))/(1-exp(-rhat_YesP_Bars*(maxOut))))) 
gen diffeuYesP_Bars = EUAYesP_Bars - EUBYesP_Bars
gen predchoiceYesP_Bars = 1 if diffeuYesP_Bars>0 & choiceleft!=.
replace predchoiceYesP_Bars =0 if diffeuYesP_Bars<0 & choiceleft!=.
tab predchoiceYesP_Bars, missing
gen corrpredYesP_Bars =0 if predchoice != choiceleft & choiceleft!=. & nopressure==0 &  Pie==0
replace corrpredYesP_Bars=1 if predchoice == choiceleft & choiceleft!=. & nopressure==0 &  Pie==0
tab corrpredYesP_Bars, missing
******************************

******************************
gen EUA_NoP_Bars= (prob1*((1-exp(-rhat_NoP_Bars*out1))/(1-exp(-rhat_NoP_Bars*(maxOut))))) 
gen EUB_NoP_Bars= (prob2*((1-exp(-rhat_NoP_Bars*out2))/(1-exp(-rhat_NoP_Bars*(maxOut))))) 
gen diffeu_NoP_Bars = EUA_NoP_Bars - EUB_NoP_Bars
gen predchoice_NoP_Bars = 1 if diffeu_NoP_Bars>0 & choiceleft!=.
replace predchoice_NoP_Bars =0 if diffeu_NoP_Bars<0 & choiceleft!=.
tab predchoice_NoP_Bars, missing
gen corrpred_NoP_Bars =0 if predchoice_NoP_Bars != choiceleft & choiceleft!=.  & nopressure==1 &  Pie==0
replace corrpred_NoP_Bars=1 if predchoice_NoP_Bars == choiceleft & choiceleft!=.  & nopressure==1 &  Pie==0
tab corrpred_NoP_Bars, missing
******************************

******************************
gen EUA_YesP_Pies= (prob1*((1-exp(-rhat_YesP_Pies*out1))/(1-exp(-rhat_YesP_Pies*(maxOut))))) 
gen EUB_YesP_Pies= (prob2*((1-exp(-rhat_YesP_Pies*out2))/(1-exp(-rhat_YesP_Pies*(maxOut))))) 
gen diffeu_YesP_Pies = EUA_YesP_Pies - EUB_YesP_Pies
gen predchoice_YesP_Pies = 1 if diffeu_YesP_Pies>0 & choiceleft!=.
replace predchoice_NoP_Bars =0 if diffeu_YesP_Pies<0 & choiceleft!=.
tab predchoice_YesP_Pies, missing
gen corrpred_YesP_Pies =0 if predchoice_YesP_Pies != choiceleft & choiceleft!=. & nopressure==0 &  Pie==1
replace corrpred_YesP_Pies=1 if predchoice_YesP_Pies == choiceleft & choiceleft!=.  & nopressure==0 &  Pie==1
tab corrpred_YesP_Pies, missing
*******************************

*******************************
gen EUA_NoP_Pies= (prob1*((1-exp(-rhat_NoP_Pies*out1))/(1-exp(-rhat_NoP_Pies*(maxOut))))) 
gen EUB_NoP_Pies= (prob2*((1-exp(-rhat_NoP_Pies*out2))/(1-exp(-rhat_NoP_Pies*(maxOut))))) 
gen diffeu_NoP_Pies = EUA_NoP_Pies - EUB_NoP_Pies
gen predchoice_NoP_Pies = 1 if diffeu_NoP_Pies>0 & choiceleft!=.
replace predchoice_NoP_Pies =0 if diffeu_NoP_Pies<0 & choiceleft!=.
tab predchoice_NoP_Pies, missing
gen corrpred_NoP_Pies =0 if predchoice_NoP_Pies != choiceleft & choiceleft!=. & nopressure==1 &  Pie==1
replace corrpred_NoP_Pies=1 if predchoice_NoP_Pies == choiceleft & choiceleft!=.  & nopressure==1 &  Pie==1
tab corrpred_NoP_Pies, missing
******************************

*Keep only the individual avg proportion of correctly predicted choices
collapse corrpredYesP_Bars corrpred_NoP_Bars corrpred_YesP_Pies corrpred_NoP_Pies Pie nopressure, by(subj)

*Average between conditions
sum corrpredYesP_Bars corrpred_NoP_Bars   corrpred_YesP_Pies  corrpred_NoP_Pies
gen corrpredCARA = (corrpredYesP_Bars + corrpred_NoP_Bars +  corrpred_YesP_Pies + corrpred_NoP_Pies)/4
*Create condition specific names of variables
gen corrpredYesP_BarsCARA = corrpredYesP_Bars
gen corrpred_NoP_BarsCARA = corrpred_NoP_Bars
gen corrpred_YesP_PiesCARA = corrpred_YesP_Pies
gen corrpred_NoP_PiesCARA = corrpred_NoP_Pies
drop corrpredYesP_Bars  corrpred_NoP_Bars    corrpred_YesP_Pies  corrpred_NoP_Pies
**********************************
*Change the name of these variables for RUM vs. RPM and CARA vs. CRRA!
**********************************
sum corrpredCARA corrpredYesP_BarsCARA corrpred_NoP_BarsCARA   corrpred_YesP_PiesCARA  corrpred_NoP_PiesCARA
save PredEURUMCARA, replace
*Adapt the program for the other 4 utilit and noise specifications.
*Files should be called:
*	PredEURUMCRRA
*	PredEURPMCARA
*	PredEURPMCRRA

